package com.sky.mapper;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

//插入一条订单数据
    @Insert("insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, " +
            "amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, " +
            "estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) VALUES " +
            "(#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus}," +
            "#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason}," +
            "#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime}," +
            "#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insert(Orders orders);

//用户付款
    @Update("UPDATE orders SET pay_method = #{payMethod}, checkout_time = #{checkoutTime}, pay_status = 1, status = 2 " +
            "WHERE number = #{orderNumber}")
    void pay(String orderNumber, int payMethod, LocalDateTime checkoutTime);
//查询出订单
    @Select("select * from orders where id = #{id}")
    Orders selectById(Integer id);
//查询当前用户的订单数量
    int selectCount(OrdersPageQueryDTO ordersPageQueryDTO);
//分页查询订单
    List<OrderVO> select(OrdersPageQueryDTO ordersPageQueryDTO, int start, int end);
//超时订单自动取消
    @Update("update orders set status = 6, cancel_time = #{now}, cancel_reason = '订单超时，自动取消' where order_time < #{time} and status = #{i}")
    void updateTimeout(int i, LocalDateTime now, LocalDateTime time);
//当天未点击送达的订单设置送达
    @Update("update orders set status = 5 where order_time > #{time} and status = #{i}")
    void updateSelivery(int i, LocalDateTime time);
//根据订单号查询id
    @Select("select id from orders where number = #{odrenNumber}")
    int selectByNumber(String orderNumber);
//工作台查询订单数
    Integer countByMap(Map map);
//工作台查询营业额
    Double sumByMap(Map map);
}
